﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using SharpDiary.Core.Data;

namespace CrumbPlus.Core.Crumb
{
    public class CrumbWorker
    {
        private CrumbInfo _curmb;
        private TimeSpan _howLong;
        private System.Timers.Timer _timer = new System.Timers.Timer();
        private DateTime _pauseStartTime;
        private bool _isPaused = false;

        public bool IsPaused
        {
            get { return _isPaused; }
        }

        /// <summary>
        /// 面包屑信息。
        /// </summary>
        public CrumbInfo Curmb
        {
            get { return _curmb; }
        }

        /// <summary>
        /// 持续的时间。
        /// </summary>
        public TimeSpan HowLong
        {
            get { return _howLong; }
        }

        private bool _isActive = false;

        /// <summary>
        /// 是否处于激活状态。
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
        }

        public event EventHandler<ElapsedEventArgs> Elasped;

        protected virtual void OnElasped(object sender, ElapsedEventArgs e)
        {
            if (Elasped != null) { Elasped(sender, e); }
        }

        public event EventHandler<CrumbWorkerStateChangedEventArgs> StateChanged;

        protected virtual void OnStateChanged(object sender, CrumbWorkerStateChangedEventArgs e)
        {
            if (StateChanged != null) { StateChanged(sender, e); }
        }

        public CrumbWorker(int interval, CrumbInfo crumb)
        {
            _curmb = crumb;
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
            _timer.Interval = interval;
        }

        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _howLong = _howLong.Add(new TimeSpan(0, 0, (int)_timer.Interval / 1000)); // e.SignalTime - _curmb.StartTime;
            OnElasped(this, e);
        }

        public void Start()
        {
            if (_isPaused) { return; }

            _isActive = true;

            _curmb.StartTime = DateTime.Now;
            _timer.Start();

            OnStateChanged(this, new CrumbWorkerStateChangedEventArgs(CrumbWorkerState.Started));
        }

        public void Pause()
        {
            if (_isPaused) { return; }

            _isPaused = true;
            _pauseStartTime = DateTime.Now;
            _timer.Stop();

            OnStateChanged(this, new CrumbWorkerStateChangedEventArgs(CrumbWorkerState.Paused));
        }

        public void Continue()
        {
            if (!_isPaused) { return; }

            _isPaused = false;
            _timer.Start();

            OnStateChanged(this, new CrumbWorkerStateChangedEventArgs(CrumbWorkerState.Continued));
        }

        public void Stop()
        {
            if (!_isActive) { return; }

            _isActive = false;

            _curmb.EndTime = DateTime.Now;
            _curmb.TotalSeconds = (long)_howLong.TotalSeconds;

            DbObjectMappingHelper.Update<CrumbInfo>(_curmb);

            _timer.Stop();

            OnStateChanged(this, new CrumbWorkerStateChangedEventArgs(CrumbWorkerState.Stopped));
        }
    }

    public enum CrumbWorkerState
    {
        Normal = 0,

        Started = 1,

        Stopped = 2,

        Paused = 3,

        Continued = 4
    }

    public class CrumbWorkerStateChangedEventArgs : EventArgs
    {
        private CrumbWorkerState _state;

        public CrumbWorkerState State
        {
            get { return _state; }
            set { _state = value; }
        }

        public CrumbWorkerStateChangedEventArgs(CrumbWorkerState state)
        {
            _state = state;
        }
    }
}
